package lecture;

import groups.ExerciseGroup;
import hibernate.HibernateUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import user.User;

/**
 *
 * @author Thorsten Ludwig
 */

@Entity
public class LectureInformation implements Serializable {
    
    private String lectureId;
    
     /**
     * name of the lecture
     */
    private String name;
    /**
     * user taken part a the lecture
     */
    @ManyToMany
    @LazyCollection(LazyCollectionOption.TRUE)
    private List<User> users;

    /**
     * List of exerciseGroups of this lecture
     */
    
    @OneToMany(cascade=CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.TRUE)
    private List<ExerciseGroup> exerciseGroups;
    
    /**
     * 
     */
    private boolean registrationOpen;
    /**
     * 
     */
    private boolean registerByTime;
    
    private int howManyExceriseGroups;
    @Id
    @GeneratedValue
    private Long id;

    public LectureInformation() {
    }
    
    
    public LectureInformation (String name) {
        this.name = name;
        this.users = new ArrayList<User>();
        this.exerciseGroups = new ArrayList<ExerciseGroup>();       
    }
    
        /**
     * return the name of the lecture
     * @return the name of the lecture
     */
    public String getName() {
        return this.name;
    }

    public boolean isRegistrationOpen() {
        return registrationOpen;
    }

    public void setRegistrationOpen(boolean isRegistrationOpen) {
        this.registrationOpen = isRegistrationOpen;
        HibernateUtil.update(this);
    }

    public int getHowManyExceriseGroups() {
        return howManyExceriseGroups;
    }

    public void setHowManyExceriseGroups(int howManyExceriseGroups) {
        this.howManyExceriseGroups = howManyExceriseGroups;
        HibernateUtil.update(this);
    }
    
    public List<ExerciseGroup> getExerciseGroups() {
        System.out.println("Versuche Gruppen zurückzugeben");
        System.out.println(this.exerciseGroups.getClass());
        List<ExerciseGroup> e = new ArrayList<ExerciseGroup>();
        for (ExerciseGroup eg: this.exerciseGroups) {
            eg.setStudents(new ArrayList<String>(eg.getStudents()));
            eg.setLeaders(new ArrayList<String>(eg.getLeaders()));
            e.add(eg);
        }
        return e;
        //return new ArrayList<ExerciseGroup> (this.exerciseGroups);
    }

    public void setExerciseGroups(List<ExerciseGroup> exerciseGroups) {
        this.exerciseGroups = exerciseGroups;
        HibernateUtil.update(this);
    }
    
    /**
     * change the lecture name
     * @param name new name
     */
    public void setName(String name) {
        this.name = name;
        HibernateUtil.update(this);
    }
    
    /**
     * 
     * @return 
     */

    
    public boolean takeUserPart(User user) {
        return this.users.contains(user);
    }
    
    public void addExerciseGroup (ExerciseGroup e) {
        this.exerciseGroups.add(e);
        HibernateUtil.update(this);
    }
    
    public void removeExerciseGroup (ExerciseGroup e) {
        this.exerciseGroups.remove(e);
        HibernateUtil.update(this);
    }
    
    public boolean isRegisterByTime() {
        return registerByTime;
    }

    public void setRegisterByTime(boolean registerByTime) {
        this.registerByTime = registerByTime;
        HibernateUtil.update(this);
    }
    
    public List<User> getUsers() {
        return users;
    }
    
    void addUser(User user) {
        this.users.add(user);
        HibernateUtil.update(this);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLectureId() {
        return lectureId;
    }

    public void setLectureId(String lectureId) {
        this.lectureId = lectureId;
        HibernateUtil.update(this);
    }

    public void setUsers(List<User> users) {
        this.users = users;
        HibernateUtil.update(this);
    }
}
